import {get, post} from './request'
import {info} from "./userinfo";
import {router} from "./router";

/**
 * 获取openid和sessionKey
 * @param code
 * @param userInfo
 * @returns {*}
 */
function postUserLogin(code, userInfo){
  const {nickName, gender, avatarUrl} = userInfo;
  return post('/user/userLogin', {code, truename: nickName, sex: gender, txLogo: avatarUrl})
}
/**
 * 获取验证码
 * @param tel 手机号
 * @returns {*}
 */
const getSMSCode = (tel) => {
  return get('/user/sendCheckCode', {tel})
};
const bindTel = (tel, code, openid) => {
  return post('/user/bindTel', {tel, code, openid})
}
const bindTelDirect = (iv, encryption, openid) => {
  return post('/user/bindTelDirect', {iv, encryption, openid})
}
const getUserHome = () => {
  return get('/user/index')
}

const getRoomInfo = (uid)=>{
  return get('/api/room/' + uid)
}

const getDynamics = (uid)=>{
  return get('/api/dynamic/' + uid)
}

const like = (uid)=> {
  if(info.getUserInfo().tel) {
    console.log("like " + uid)
    return post('/api/protect/like', {
      roomOwnerId: uid
    }, true)
  } else {
    router.toRegister();
    return null;
  }
}

const getPayInfo = (grade)=> {

}

export const api = {
  postUserLogin,
  getSMSCode,
  bindTel,
  bindTelDirect,
  getUserHome,
  getRoomInfo,
  like,
  getDynamics
}
